package com.ymatou.iapi.productprice.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.FileUtil;
import com.ymttest.utils.YMTDateUtil;

public class MainTest {
	private static int poolSize = Integer.parseInt(EnvSetup.getData("productprice_poolsize"));
	private static int maxPoolSize = Integer.parseInt(EnvSetup.getData("productprice_maxpoolsize"));
	
	
	private static int group = 6000;
	
	private static 	ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(poolSize  ,maxPoolSize, Long.MAX_VALUE, TimeUnit.SECONDS,new LinkedBlockingQueue<>());

	public static void runTask(List<Runnable> runList){
		Date startDate = YMTDateUtil.getDate();
		
		runList.forEach(ele->{
			threadPoolExecutor.execute(ele);
		});
		
		threadPoolExecutor.shutdown();// 只是不能再提交新任务，等待执行的任务不受影响

		
		try {
			boolean loop = true;
			do { // 等待所有任务完成
				loop = !threadPoolExecutor.awaitTermination(1, TimeUnit.SECONDS); // 阻塞，直到线程池里所有任务结束
			} while (loop);

		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		
		Date endDate = YMTDateUtil.getDate();
		System.out.println((endDate.getTime() - startDate.getTime()) / 1000);
		System.out.println(endDate);
	}
	
	
	public static List<Runnable> compareApi(List<Integer> userIdList,List<String> prodOrCatalogIds,BiConsumer<List<Integer>, List<String>> function){
		//数据分组
		List<List<String>> productsFromFileAry = ComparePrice.splitCollectionToCollectionList(prodOrCatalogIds, group);
		
		List<Runnable> runList = new ArrayList<>();
		for (int i = 0; i < productsFromFileAry.size(); i++) {
			List<String> tmp = productsFromFileAry.get(i);
			runList.add(()->{
				function.accept(userIdList, tmp);
			});
		}
		return runList;
		
	}
	
	public static void main(String[] args)  {
		
		String folder = ComparePrice.folder;
		String errorFile = ComparePrice.errorFile;
		String diffFile = ComparePrice.diffFile;
		String retFile = ComparePrice.retFile;
		
		
		FileUtil.deleteFile(errorFile);
		FileUtil.deleteFile(diffFile);
		FileUtil.deleteFile(retFile);
		
		String userPath = folder + "\\userId.txt";
		String productPath = folder + "\\productId.txt";
		String catalogPath = folder +"\\catalogId.txt"; 
		
		//读取数据
		List<Integer> userIdsFromFile = new ArrayList<>();
		FileUtil.readFileAsLine(userPath).forEach(ele -> userIdsFromFile.add(Integer.parseInt(ele)));
		List<String> productsFromFile = FileUtil.readFileAsLine(productPath);
		List<String>  catalogsFromFile = FileUtil.readFileAsLine(catalogPath);
		
		
		//分组
		//List<List<String>> productsFromFileAry = ComparePrice.splitCollectionToCollectionList(productsFromFile, group);
	 	//List<List<String>> catalogsFromFileAry = ComparePrice.splitCollectionToCollectionList(catalogsFromFile, group);
		
		List<Runnable> runList = new ArrayList<>();
		String k = args[0];
		if (k.equals("1")){
			runList.addAll(compareApi(userIdsFromFile,productsFromFile,(x,y)->ComparePrice.checkPriceByProd(x,y)));
		}
		
		if (k.equals("2")){
			runList.addAll(compareApi(userIdsFromFile,catalogsFromFile,(x,y)->ComparePrice.checkPriceByCatalog(x,y)));
		}
		
		if (k.equals("3")){
			runList.addAll(compareApi(userIdsFromFile,catalogsFromFile,(x,y)->ComparePrice.checkPriceByMultiLogistics(x,y)));
		}
	 	
	 	if (k.equals("4")){
	 		
	 		//runList.addAll(compareApi(userIdsFromFile,catalogsFromFile,(x,y)->ComparePrice.checkPriceByCatalog(x,y)));
	 		runList.addAll(compareApi(userIdsFromFile,catalogsFromFile,(x,y)->ComparePrice.checkPriceByMultiLogistics(x,y)));
	 		runList.addAll(compareApi(userIdsFromFile,productsFromFile,(x,y)->ComparePrice.checkPriceByProd(x,y)));
	 	}
	 	 
		runTask(runList);
	 	
	}
}
